/*
Copyright 2014, KISSY v1.47
MIT Licensed
build time: May 22 12:26
*/
KISSY.add("editor/plugin/undo/cmd",["editor"],function(f,j){function h(a){var c=a.get("document")[0].body.innerHTML,b;c&&(b=a.getSelection());this.contents=c;this.bookmarks=b&&b.createBookmarks2(!0)}function i(a){this.history=[];this.index=-1;this.editor=a;this.bufferRunner=f.buffer(this.save,500,this);this._init()}var g=j("editor"),k=f.UA;f.augment(h,{equals:function(a){return this.contents===a.contents}});var l={16:1,17:1,18:1},m={37:1,38:1,39:1,40:1,33:1,34:1};f.augment(i,{_keyMonitor:function(){var a=
this,c=a.editor;c.docReady(function(){c.get("document").on("keydown",function(b){var d=b.keyCode;d in m||d in l||(90===d&&(b.ctrlKey||b.metaKey)?(!1!==c.fire("beforeRedo")&&a.restore(-1),b.halt()):89===d&&(b.ctrlKey||b.metaKey)?(!1!==c.fire("beforeUndo")&&a.restore(1),b.halt()):!1!==c.fire("beforeSave",{buffer:1})&&a.save(1))})})},_init:function(){var a=this,c=a.editor;a._keyMonitor();setTimeout(function(){if(c.get("mode")===g.Mode.WYSIWYG_MODE)if(c.isDocReady())a.save();else c.on("docReady",function d(){a.save();
c.detach("docReady",d)})},0)},save:function(a){var c=this.editor;if(c.get("mode")===g.Mode.WYSIWYG_MODE&&c.get("document"))if(a)this.bufferRunner();else{var a=this.history,b=a.length,d=this.index,b=Math.min(b,d+1),d=a[b-1],e=new h(c);if(!d||!d.equals(e))a.length=b,30===b&&(a.shift(),b--),a.push(e),this.index=d=b,c.fire("afterSave",{history:a,index:d})}},restore:function(a){if(this.editor.get("mode")===g.Mode.WYSIWYG_MODE){var c=this.history,b=this.editor,d=b.get("document")[0].body,e=c[this.index+
a];e&&(d.innerHTML=e.contents,e.bookmarks?b.getSelection().selectBookmarks(e.bookmarks):k.ie&&(d=d.createTextRange(),d.collapse(!0),d.select()),(d=b.getSelection())&&d.scrollIntoView(),this.index+=a,b.fire(0>a?"afterUndo":"afterRedo",{history:c,index:this.index}),b.notifySelectionChange());return e}}});return{init:function(a){if(!a.hasCommand("save")){var c=new i(a);a.addCommand("save",{exec:function(b,d){a.focus();c.save(d)}});a.addCommand("undo",{exec:function(){a.focus();c.restore(-1)}});a.addCommand("redo",
{exec:function(){a.focus();c.restore(1)}})}}}});
